package com.qf.business.extentions;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.User;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
@WebFilter(filterName = "businessFilter", urlPatterns = {"/*"})
public class BusinessFilter implements Filter {


    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        String userJson = req.getHeader("user");
        System.out.println("------------------BusinessFilter doFilter");
        System.out.println(userJson);
        if (null == userJson || userJson.isEmpty()) {
            chain.doFilter(request, response);
            return;
        }
        //获取用户信息存入本地线程变量中
        User user = JSON.parseObject(userJson, User.class);

        UserInfoUtil.putUserInfo(user);
        chain.doFilter(request, response);
    }
}
